﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class UserOrderList : BasePage
    {
        int CurrentPage = 1;
        protected void Page_Load( object sender, EventArgs e )
        {
            Check();
            hidActive.Value = Request.QueryString["act"];
            if (!int.TryParse(Request.QueryString["page"], out CurrentPage))
            {
                CurrentPage = 1;
            }
            hidPage.Value = CurrentPage.ToString();

            AjaxProcess();
        }

        protected void AjaxProcess()
        {
            if (Request.QueryString["sync"] == "true")
            {
                Response.Write(GetListJson());
                Response.End();
            }
        }

        protected string GetListJson()
        {
            BLL.Orders ordBLL = new BLL.Orders();
            string strWhere = "IsActive=1 and UserId=" + this.UserId;
            if (Request.QueryString["act"] == "pay")
            {
                strWhere += " and Status=2";
            }
            else if (Request.QueryString["act"] == "sent")
            {
                strWhere += " and Status=4";
            }
            string strOrder = "OrderId desc";
            int recordCount = ordBLL.GetRecordCount(strWhere);
            int startIndex = 0;
            int endIndex = 0;
            int total = 0;
            if (recordCount <= 10)
            {
                total = 1;
                startIndex = 1;
                endIndex = recordCount;
            }
            else
            {
                total = recordCount % 10 == 0 ? recordCount / 10 : recordCount / 10 + 1;
                startIndex = 10 * (CurrentPage - 1) + 1;
                endIndex = 10 * CurrentPage;
            }
            DataSet ds = ordBLL.GetListByPageV(strWhere, strOrder, startIndex, endIndex);
            StringBuilder strBuilder = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (strBuilder.ToString() != "")
                {
                    strBuilder.Append(",");
                }
                strBuilder.Append("{");
                strBuilder.Append("\"OrderId\":\"" + dr["OrderId"] + "\",\"OrderNo\":\"" + dr["OrderNo"] + "\",\"ProductNum\":\"" + dr["OitemNum"] + "\",\"Total\":\"" + dr["Total"] + "\",\"Status\":\"" + dr["Status"] + "\"");
                strBuilder.Append(",\"oitem\":[" + GetOitemJson(DigitHelper.GetInt32(dr["OrderId"])) + "]");
                strBuilder.Append("}");
            }

            return "{\"code\":1,\"msg\":\"\",\"data\":[" + strBuilder.ToString() + "]}";
        }

        protected string GetOitemJson(int orderId)
        {
            BLL.Oitems oitBLL = new BLL.Oitems();
            DataSet ds = oitBLL.GetList("IsActive=1 and OrderId=" + orderId);
            StringBuilder strBuilder = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (strBuilder.ToString() != "")
                {
                    strBuilder.Append(",");
                }
                strBuilder.Append("{");
                strBuilder.Append("\"Pic\":\"" + dr["ProductPic"] + "\",\"ProductName\":\"" + dr["ProductName"] + "\",\"ProductNum\":\"" + dr["ProductNum"] + "\",\"ProductPrice\":\"" + dr["TotalPrice"] + "\",\"Desc\":\"" + dr["ProductDesc"] + "\"");
                strBuilder.Append("}");
            }

            return strBuilder.ToString();
        }
    }
}